//
//  HotPlaceViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-4-6.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HotPlaceViewController.h"
#import "HotPlace_DetilCollectionViewCell.h"
#import "JourneryNetHandler.h"
#import "JourneyDetilViewController.h"
#import "MBProgressHUD.h"

@interface HotPlaceViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,MBProgressHUDDelegate>
@property (nonatomic , retain)UICollectionView *hotPlaceView;
@property (nonatomic, retain)NSMutableArray *hotplaceArr;
@property (nonatomic , retain)MBProgressHUD *progress;

@end

@implementation HotPlaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self netHander];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width - 30, 130);
    //最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    //最小行间距
    flowLayout.minimumLineSpacing = 10;
    //边缘距离
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 15);
    
    self.hotPlaceView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _hotPlaceView.backgroundColor = [UIColor colorFromHexCode:@"#FAF0E6"];
    _hotPlaceView.delegate = self;
    _hotPlaceView.dataSource = self;
    [self.view addSubview:_hotPlaceView];
    
    [_hotPlaceView registerClass:[HotPlace_DetilCollectionViewCell class] forCellWithReuseIdentifier:@"热门地点"];
    
    
    //添加菊花的时候要注意是否被别的视图盖上
    
    self.progress = [[MBProgressHUD showHUDAddedTo:self.view animated:YES]retain];
    [_progress show:YES];
    self.progress.delegate = self;
    
    [_hotPlaceView release];
    [flowLayout release];
}

- (void)netHander
{
    //    NSString *la = [NSString stringWithFormat:@"%@",[self.location objectForKey:@"lat"]];
    //    NSString *lon = [NSString stringWithFormat:@"%@",[self.location objectForKey:@"lng"]];
    
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/pois/all/?sort=default&start=0&latitude=%@&longitude=%@",self.type,self.idNumber,self.la,self.lon];
    self.hotplaceArr = [NSMutableArray array];
    [JourneryNetHandler getDataWithUrl:str completion:^(NSDictionary *data) {
        
        NSArray *arr = [data objectForKey:@"items"];
        self.hotplaceArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in arr) {
            HotPlaceDataSource *data = [[HotPlaceDataSource alloc]initWithDictionary:dic];
            
            [self.hotplaceArr addObject:data];
            [data release];
        }
        [self.hotPlaceView reloadData];
        [self.progress hide:YES];
    }];
}
#pragma  mark collectiondeleget
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.hotplaceArr count];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotPlace_DetilCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"热门地点" forIndexPath:indexPath];
    
    cell.hotData = [self.hotplaceArr objectAtIndex:indexPath.item];
    //    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JourneyDetilViewController *jourVC = [[JourneyDetilViewController alloc]init];
    jourVC.journeyData = [self.hotplaceArr objectAtIndex:indexPath.item];
    [self.navigationController pushViewController:jourVC animated:NO];
    [jourVC release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark - Hide MB


- (void)hudWasHidden:(MBProgressHUD *)hud
{
    
    [_progress removeFromSuperview];
    [_progress release];
    _progress  = nil;
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
